<?php 
namespace app\wx\controller;

use think\Exception;
use think\Controller;

use app\wx\model\Book as BookModel;
use app\wx\model\Comment as CommentModel;
use app\wx\model\Favor as FavorModel;
use app\wx\model\Hot as HotModel;
class Book extends Controller {
  public function getBookList() {
    $book = new BookModel();
    $data = $book::page($this->request->param("page"),$this->request->param("pageSize"))->select();
    if($data) {
      return json([
        "msg"=> "查找成功",
        "code"=> 20000,
        "data"=> $data
      ],200);
    }
  }

  public function addBook() {
    // 判断是否有id
    if($this->request->param('id')) {
      $pro = BookModel::get($this->request->param('id'));
    }else {
      $pro = new BookModel();
    }
    $file = request()->file('img');
    // 移动到框架应用根目录/uploads/ 目录下
    $info = $file->move( './uploads');
    if($info){
        // 成功上传后 获取上传信息
        $pro->image =  $info->getSaveName();
        $pro->content = $this->request->param('content');
        // $pro->like_status = $this->request->param('like_status');
        $pro->title = $this->request->param('title');
        $pro->author = $this->request->param('author');
        $pro->isbn = $this->request->param('isbn');
        $pro->price = $this->request->param('price');
        $pro->subtitle = $this->request->param('subtitle');
        $pro->publisher = $this->request->param('publisher');
        $pro->type = 400;
        $data = $pro->save();
        if($data) {
            return json([
                "msg"=> "success",
                "code"=>20000
            ],200);
        }
    }else{
        // 上传失败获取错误信息
        echo $file->getError();
    }
  }

  public function getDetail() {
    $book = BookModel::get($this->request->param('id'));
    if($book) {
      return json([
          "msg"=> "success",
          "data"=> $book,
          "code"=>20000
      ],200);
    }
  }

  public function getComment() {
    $commentModel = new CommentModel();
    $data = $commentModel::where('book_id', $this->request->param('id'))->select();
    if($data) {
      return json([
        "msg"=> "查找成功",
        "code"=> 20000,
        "data"=> $data
      ],200);
    }
  }

  public function getFavor() {
    $favor = new FavorModel();
    $count = $favor::where('favor_id', $this->request->param('id'))->where('type', 400)->select()->count();
    $data = $favor::where('favor_id', $this->request->param('id'))->where('user', $this->request->param('user'))->where('type', 400)->find();
    $result = [];
    $result["fav_nums"] = $count;
    $result["like_status"] = 0;
    if($data) {
      $result["like_status"] = 1;
    }
    return json([
      "msg"=> "查找成功",
      "code"=> 20000,
      "data"=> $result
    ],200);
  }

  public function getHot() {
    $hot = new HotModel();
    $data = $hot::select();
    return json([
      "msg"=> "查找成功",
      "code"=> 20000,
      "data"=> $data
    ],200);
  }

  public function searchBook() {
    $book = new BookModel();
    $data = $book->where("title", 'like', '%'.$this->request->param("search").'%')->whereOr('author', $this->request->param("search"))->page($this->request->param("page"),$this->request->param("pageSize"))->order('id', 'desc')->select();
    $count = $book->where("title", 'like', '%'.$this->request->param("search").'%')->whereOr('author', $this->request->param("search"))->select()->count();
    $result["books"] = $data;
    $result["page"] = $this->request->param("page");
    $result["pageSize"] = $this->request->param("pageSize");
    $result["total"] = $count;
    return json([
      "msg"=> "查找成功",
      "code"=> 20000,
      "data"=> $result
    ],200);
  }
}